<template>
  <view>
    <u-navbar :title="query.id ? '企业人员' : '新增企业人员'"> </u-navbar>
    <formz ref="formz" :inputs="inputs" :data="data"></formz>

    <bottombox>
      <template v-if="query.id">
        <u-button
          type="error"
          :loading="loading"
          text="删除"
          @click="ondel"
        ></u-button>
        <view style="width: 40rpx"></view>
      </template>
      <u-button
        type="primary"
        :loading="loading"
        text="保存数据"
        @click="onedit"
      ></u-button>
    </bottombox>
  </view>
</template>

<script>
import { add, update, remove, getInfo } from "@/api/company/empInfo";
export default {
  data() {
    return {
      data: {},
      loading: false,
      inputs: [
        {
          label: "部门",
          value: "deptId",
          type: "deptId",
          rules: [
            {
              required: true,
              trigger: "blur",
              message: "请选择部门",
            },
          ],
        },
        {
          label: "人员名称",
          value: "name",
          rules: [
            {
              required: true,
              message: "用户名称不能为空",
              trigger: "blur",
            },
          ],
        },
        {
          label: "岗位",
          value: "job",
          type: "select",
          option:
            this.$store.state.dict.dictDatas[
              this.$store.state.dict.DICT_TYPE.JOB_POST
            ],
        },
        {
          label: "性别",
          value: "sex",
          type: "select",
          option:
            this.$store.state.dict.dictDatas[
              this.$store.state.dict.DICT_TYPE.SYSTEM_USER_SEX
            ],
          rules: [
            {
              required: true,
              message: "性别不能为空",
              trigger: "change",
            },
          ],
        },
        {
          label: "身份证号",
          value: "cardNum",
          rules: [
            {
              required: true,
              message: "身份证号不能为空",
              trigger: "blur",
            },
            {
              pattern: /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/,
              message: "请输入正确的身份证号",
              trigger: "blur",
            },
          ],
        },
        {
          label: "手机号",
          value: "telNum",
          rules: [
            {
              required: true,
              message: "手机号不能为空",
              trigger: "blur",
            },
            {
              pattern:
                /^(?:(?:\+|00)86)?1(?:3[\d]|4[5-79]|5[0-35-9]|6[5-7]|7[0-8]|8[\d]|9[189])\d{8}$/,
              message: "请输入正确的手机号",
              trigger: "blur",
            },
          ],
        },
        {
          label: "毕业院校",
          value: "school",
        },
        {
          label: "专业",
          value: "major",
        },
        {
          label: "职工状态",
          value: "status",
          type: "select",
          option:
            this.$store.state.dict.dictDatas[
              this.$store.state.dict.DICT_TYPE.EMPLOYEE_STATUS
            ],
          rules: [
            {
              required: true,
              message: "职工状态不能为空",
              trigger: "change",
            },
          ],
        },
        {
          label: "入职时间",
          value: "entryTime",
          type: "datetime",
        },
        {
          label: "离职时间",
          value: "dimissionTime",
          type: "datetime",
        },
      ],
      query: {},
    };
  },
  onLoad(query) {
    this.query = query;
    this.init();
  },
  methods: {
    async init() {
      if (this.query.id) {
        let data = await getInfo(this.query);
        this.data = data.data;
      } else {
        this.data = {};
      }
      console.log(this.data);
    },
    async ondel() {
      uni.showModal({
        title: "提示",
        content: "确定删除此数据？",
        success: async (res) => {
          if (res.confirm) {
            this.loading = true;
            await remove([this.data.id]);
            uni.showToast({
              title: "操作成功",
              icon: "none",
              duration: 2000,
            });
            this.loading = false;
            uni.navigateBack();
          } else if (res.cancel) {
            console.log("用户点击取消");
          }
        },
      });
    },
    async onedit() {
      let data = await this.$refs.formz.validate();

      this.loading = true;
      try {
        if (data.id) {
          await update(data);
          await this.$u.modal("修改成功");
        } else {
          await add(data);
          await this.$u.modal("添加成功");
        }
        this.loading = false;
        this.$u.back();
      } catch (error) {
        this.loading = false;
      }
    },
  },
};
</script>

<style>
page {
  background: #fff;
}
</style>
